package cn.iocoder.yudao.module.enterprise.controller.admin.praxisProject;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.enterprise.controller.admin.praxisProject.vo.*;
import cn.iocoder.yudao.module.enterprise.service.praxisProject.EnterpriseProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 -企业端- 比赛管理")
@RestController
@RequestMapping("/enterprise/entry-info")
@Validated
public class EnterpriseEntryInfoController {

    @Resource
    private EnterpriseProjectService enterpriseProjectService;


    @PostMapping("/getEntryInfoPage")
    @ApiOperation("(企业端)获得比赛管理分页")
    public CommonResult<PageResult<EnterpriseEntryInfoPageRespVO>> getEntryInfoPage(@Valid @RequestBody EnterpriseEntryInfoPageReqVO pageVO) {
        SortingField sortField = pageVO.getSortField();
        if(null==sortField){
            sortField=new SortingField();
            sortField.setOrder("desc");
            sortField.setField("t.weight");
            pageVO.setSortField(sortField);
        }
        PageResult<EnterpriseEntryInfoPageRespVO> pageResult = enterpriseProjectService.getEntryInfoPage(pageVO);
        return success(pageResult);
    }
    @PostMapping("/getEntryInfoPageByProjectId")
    @ApiOperation("(企业端)根据项目ID获得比赛管理分页")
    public CommonResult<PageResult<EnterpriseEntryStudentPageRespVO>> getEntryInfoPageByProjectId(@Valid @RequestBody EnterpriseEntryStudentPageReqVO pageVO) {
        SortingField sortField = pageVO.getSortField();
        if(null==sortField){
            sortField=new SortingField();
            sortField.setOrder("desc");
            sortField.setField("t.update_time");
            pageVO.setSortField(sortField);
        }
        PageResult<EnterpriseEntryStudentPageRespVO> pageResult = enterpriseProjectService.getEntryInfoPageByProjectId(pageVO);
        return success(pageResult);
    }



    @GetMapping("/getEntryInfoById")
    @ApiOperation("(企业端)获取报名信息")
    @ApiImplicitParam(name = "id", value = "报名ID", required = true, dataTypeClass = Long.class)
    public CommonResult<EnterpriseEntryInfoBaseVO> getEntryInfoById(@RequestParam("id") Long id) {
        EnterpriseEntryInfoBaseVO entryInfo  = enterpriseProjectService.getEntryInfoById(id);
        if(null!=entryInfo && null!=entryInfo.getFirstEntryId()){
            List<EnterpriseEntryStudentVO> entryStudentList = enterpriseProjectService.getEntryStudentList(entryInfo.getFirstEntryId());
            entryInfo.setStuList(entryStudentList);
        }
        return success(entryInfo);
    }

    @PostMapping("/updateRateScore")
    @ApiOperation("(企业端)评分")
    @TenantIgnore
    public CommonResult<Boolean> updateRateScore(@Valid @RequestBody EnterpriseProjectSourceUpdateVO updateVO) {
        enterpriseProjectService.updateRateScore(updateVO);
        return success(true);
    }

    @PostMapping("/batchUpdateStatus")
    @ApiOperation("(企业端)入选/撤销")
    public CommonResult<Boolean> batchUpdateStatus(@Valid @RequestBody EnterpriseProjectUpdateVO updateVO) {
        enterpriseProjectService.batchUpdateStatus(updateVO);
        return success(true);
    }



    @GetMapping("/getNextScheduleByProjectId")
    @ApiOperation("(企业端)入选获得下一个阶段比赛信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目ID", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "scheduleType", value = "类型;1:遴选;2:初赛;3:复赛;4:决赛;",required = true,   dataTypeClass = Integer.class)
    })
    public CommonResult<EnterpriseProjectScheduleVO> getNextScheduleByProjectId(@RequestParam("projectId") Long projectId,@RequestParam("scheduleType") Integer scheduleType) {
        return success(enterpriseProjectService.getNextScheduleByProjectId(projectId,scheduleType));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出比赛管理 Excel")
    @OperateLog(type = EXPORT)
    public void exportEnterpriseEntryExcel ( EnterpriseEntryStudentExcelReqVO exportReqVO,
                                         HttpServletResponse response) throws IOException {
        List<EnterpriseEntryStudentPageRespVO> list = enterpriseProjectService.getEntryInfoPageByProjectList(exportReqVO);
        List<EnterpriseEntryStudentExcelRespVO> datas =new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)){
            list.stream().forEach(m->{
                EnterpriseEntryStudentExcelRespVO excelVO=new EnterpriseEntryStudentExcelRespVO();
                BeanUtils.copyProperties(m,excelVO);
                if(null!=m.getBirthday()){
                    int age = DateUtils.getAge(m.getBirthday());
                    excelVO.setAge(age);
                }
                if(null!=m.getLookStatus() && m.getLookStatus()==2){
                    excelVO.setLookStatusText("已查看");
                }else{
                    excelVO.setLookStatusText("未查看");
                }
                if(4==exportReqVO.getScheduleType()){
                    if(null!=m.getCheckStatus() && m.getCheckStatus()==2){
                        excelVO.setCheckStatusText("获奖");
                    }else{
                        excelVO.setCheckStatusText("未获奖");
                    }
                }else{
                    if(null!=m.getCheckStatus() && m.getCheckStatus()==2){
                        excelVO.setCheckStatusText("入选");
                    }else{
                        excelVO.setCheckStatusText("未入选");
                    }
                }
                excelVO.setEntryDate(DateUtils.format(m.getEntryTime(),DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
                excelVO.setUpdateDate(DateUtils.format(m.getUpdateTime(),DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
                datas.add(excelVO);
            });
        }
        // 导出 Excel
        ExcelUtils.write(response, "比赛管理.xls", "数据", EnterpriseEntryStudentExcelRespVO.class, datas);
    }


}
